package org.jeecg.modules.demo.crm.service.impl;

import com.unfbx.chatgpt.exception.BaseException;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CProjectQuote;
import org.jeecg.modules.demo.crm.entity.CSubProject;
import org.jeecg.modules.demo.crm.mapper.CProjectQuoteMapper;
import org.jeecg.modules.demo.crm.mapper.CSubProjectMapper;
import org.jeecg.modules.demo.crm.service.ICProjectQuoteService;
import org.jeecg.modules.demo.crm.service.ICSubProjectStatusLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 项目报价表
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Service
public class CProjectQuoteServiceImpl extends ServiceImpl<CProjectQuoteMapper, CProjectQuote> implements ICProjectQuoteService {

    @Autowired
    private CProjectQuoteMapper cProjectQuoteMapper;

    @Autowired
    CSubProjectMapper cSubProjectMapper;

    @Autowired
    ICSubProjectStatusLogService cSubProjectStatusLogService;

    @Override
    public void addQuote(CProjectQuote cProjectQuote) {
        CSubProject cSubProject = cSubProjectMapper.selectById(cProjectQuote.getSubProjectId());
        //判断状态--只有待报价才能提交
        /*if( !"报价中".equals(cSubProject.getStatus())){
          throw new BaseException("项目状态异常");
        }*/

        //修改其他报价记录为失效状态
        cProjectQuoteMapper.updateStatus(cProjectQuote.getSubProjectId(),"失效");
        cProjectQuote.setStatus("审核中");
        cProjectQuoteMapper.insert(cProjectQuote);
        //修改子项目状态为 审核中
        cSubProject.setStatus("审核中");
        cSubProject.setTotalQuotation(cProjectQuote.getContractPrice());
        cSubProjectStatusLogService.updateStatus(cSubProject.getId(),cSubProject.getStatus());
        cSubProjectMapper.updateById(cSubProject);

    }

    @Override
    public void edit(CProjectQuote cProjectQuote) {
        CSubProject cSubProject = cSubProjectMapper.selectById(cProjectQuote.getSubProjectId());
        if("审核中".equals(cProjectQuote.getStatus())){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            CProjectQuote cProjectQuote1 = cProjectQuoteMapper.selectById(cProjectQuote.getId());

            cProjectQuote1.setContractPrice(cProjectQuote.getContractPrice());
            cProjectQuote1.setTaxInclusivePrice(cProjectQuote.getTaxInclusivePrice());
            cProjectQuote1.setTaxRate(cProjectQuote.getTaxRate());
            cProjectQuote1.setConstructionPlan(cProjectQuote.getConstructionPlan());
            cProjectQuote1.setQuotationFile(cProjectQuote.getQuotationFile());
            cProjectQuote1.setRemark(cProjectQuote.getRemark());
            cProjectQuote1.setStatus("审核中");
            cProjectQuote1.setUserId(sysUser.getId());
            cProjectQuote1.setTaxIncluded(cProjectQuote.getTaxIncluded());
            cProjectQuote1.setAdditionalId(cProjectQuote.getAdditionalId());
            cProjectQuote1.setReviewDescription("");
            cProjectQuote1.setReviewTime(null);
            cProjectQuote1.setReviewUserId(null);
            cProjectQuoteMapper.updateById(cProjectQuote1);



            //修改子项目状态为 审核中
            cSubProject.setStatus("审核中");
            cSubProject.setTotalQuotation(cProjectQuote.getContractPrice());
            cSubProjectStatusLogService.updateStatus(cSubProject.getId(),cSubProject.getStatus());
            cSubProjectMapper.updateById(cSubProject);


        }else {
            BigDecimal totalQuotation = cProjectQuote.getContractPrice();
            CProjectQuote projectQuote = cProjectQuoteMapper.selectById(cProjectQuote.getId());
            if("审核中".equals(projectQuote.getStatus())){
                String status = "";
                if("驳回".equals(cProjectQuote.getStatus())){
                    status = "驳回";
                    totalQuotation = BigDecimal.ZERO;
                }else if("同意".equals(cProjectQuote.getStatus())){
                    status = "报价完成";
                }else {
                    throw new RuntimeException("状态异常");
                }
                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                cProjectQuote.setReviewDescription(cProjectQuote.getReviewDescription());
                cProjectQuote.setReviewTime(new Date());
                cProjectQuote.setStatus(status);
                cProjectQuote.setReviewUserId(sysUser.getId());
                cProjectQuoteMapper.updateById(cProjectQuote);
                //将所有
                // cProjectQuoteMapper.updateStatus(cProjectQuote.getSubProjectId(),status);

                if ("报价完成".equals(status)){
                    //修改子项目状态为 报价完成
                    cSubProject.setStatus("报价完成");
                    cSubProjectStatusLogService.updateStatus(cSubProject.getId(),cSubProject.getStatus());
                    cSubProjectMapper.updateById(cSubProject);
                }else {
                    //修改子项目状态为 报价完成
                    cSubProject.setStatus("报价中");
                    cSubProject.setTotalQuotation(totalQuotation);
                    cSubProjectStatusLogService.updateStatus(cSubProject.getId(),cSubProject.getStatus());
                    cSubProjectMapper.updateById(cSubProject);
                }

            }
        }


    }

    @Override
    public void addAdditional(CProjectQuote cProjectQuote) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CProjectQuote cProjectQuoteV2 = new CProjectQuote();
        cProjectQuoteV2.setContractPrice(cProjectQuote.getContractPrice());
        cProjectQuoteV2.setTaxInclusivePrice(cProjectQuote.getTaxInclusivePrice());
        cProjectQuoteV2.setTaxRate(cProjectQuote.getTaxRate());
        cProjectQuoteV2.setConstructionPlan(cProjectQuote.getConstructionPlan());
        cProjectQuoteV2.setQuotationFile(cProjectQuote.getQuotationFile());
        cProjectQuoteV2.setRemark(cProjectQuote.getRemark());
        cProjectQuoteV2.setStatus("审核中");
        cProjectQuoteV2.setUserId(sysUser.getId());
        cProjectQuoteV2.setAdditionalId(cProjectQuote.getAdditionalId());
        cProjectQuoteV2.setTaxIncluded(cProjectQuote.getTaxIncluded());
        cProjectQuoteMapper.insert(cProjectQuoteV2);

    }

    @Override
    public void editAdditional(CProjectQuote cProjectQuote) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CProjectQuote cProjectQuote1 = cProjectQuoteMapper.selectById(cProjectQuote.getId());

        cProjectQuote1.setContractPrice(cProjectQuote.getContractPrice());
        cProjectQuote1.setTaxInclusivePrice(cProjectQuote.getTaxInclusivePrice());
        cProjectQuote1.setTaxRate(cProjectQuote.getTaxRate());
        cProjectQuote1.setConstructionPlan(cProjectQuote.getConstructionPlan());
        cProjectQuote1.setQuotationFile(cProjectQuote.getQuotationFile());
        cProjectQuote1.setRemark(cProjectQuote.getRemark());
        cProjectQuote1.setStatus("审核中");
        cProjectQuote1.setUserId(sysUser.getId());
        cProjectQuote1.setTaxIncluded(cProjectQuote.getTaxIncluded());
        cProjectQuote1.setAdditionalId(cProjectQuote.getAdditionalId());
        cProjectQuote1.setReviewDescription("");
        cProjectQuote1.setReviewTime(null);
        cProjectQuote1.setReviewUserId(null);
        cProjectQuoteMapper.updateById(cProjectQuote1);

    }

    @Override
    public void auditAdditional(CProjectQuote cProjectQuote) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CProjectQuote cProjectQuote1 = cProjectQuoteMapper.selectById(cProjectQuote.getId());
        cProjectQuote1.setStatus(cProjectQuote.getStatus());
        cProjectQuote1.setReviewTime(new Date());
        cProjectQuote1.setReviewUserId(sysUser.getId());
        cProjectQuote1.setReviewDescription(cProjectQuote.getReviewDescription());
        cProjectQuoteMapper.updateById(cProjectQuote1);
    }
}
